import java.io.IOException;
import java.nio.*;
import java.nio.channels.DatagramChannel;
import java.net.*;

public class Test {

	public void pingMe() {

		try {
			ByteBuffer send = ByteBuffer.wrap("Hello".getBytes());
			ByteBuffer receive = ByteBuffer.allocate("Hello".getBytes().length);
			// use echo port 7
			InetSocketAddress socketAddress = new InetSocketAddress(
					"4.2.2.2", 7);
			DatagramChannel dgChannel = DatagramChannel.open();
			// we have the channel non-blocking.
			dgChannel.configureBlocking(false);
			dgChannel.connect(socketAddress);
			dgChannel.send(send, socketAddress);
			/*
			 * it's non-blocking so we need some amount of delay to get the
			 * response
			 */
			Thread.sleep(100);
			dgChannel.receive(receive);
			String response = new String(receive.array());
			if (response.equals("Hello")) {
				System.out.println("Ping is alive");
			} else {
				System.out.println("No response");
			}

		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}
}
